package com.zone.vehicle.superApps.datasource;

import com.google.gson.Gson;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.account.entity.CaptchaCertExchangeEntity;
import com.zone.sdk.module.account.entity.CaptchaGetEntity;
import com.zone.sdk.module.account.entity.CheckCaptchaEntity;
import com.zone.sdk.module.account.entity.LoginEntity;
import com.zone.sdk.module.account.entity.LoginStatusEntity;
import com.zone.sdk.module.account.entity.QrLoginEntity;
import com.zone.sdk.module.account.entity.UserInfoEntity;
import com.zone.sdk.module.account.request.CaptchaCertExchangeRequest;
import com.zone.sdk.module.account.request.CaptchaGetRequest;
import com.zone.sdk.module.account.request.CheckCaptchaRequest;
import com.zone.sdk.module.account.request.LoginRequest;
import com.zone.sdk.module.account.request.LogoutRequest;
import com.zone.sdk.module.account.request.QrLoginRequest;
import com.zone.sdk.module.account.request.UserInfoRequest;
import com.zone.sdk.module.account.response.CaptchaGetResponse;
import com.zone.sdk_api.ApiManager;
import com.zone.vehicle.lib.view.captcha.model.Point;
import com.zone.vehicle.lib.view.utils.AESUtil;
import com.zone.vehicle.superApps.utils.Sha256;

/**
 * Class that handles authentication w/ login credentials and retrieves user information.
 */
public class LoginDataSource {


    
    public void captchaGet(ResultCallBack<CaptchaGetResponse<CaptchaGetEntity>> callback){
        CaptchaGetRequest request = new CaptchaGetRequest();
        request.setCaptchaType("blockPuzzle");
        ApiManager.getInstance().captchaGet(request, callback);
    }

    /**
     * 三方安全校验
     */
    public void checkCaptcha(double sliderXMoved, String token, String key, ResultCallBack<CaptchaGetResponse<CheckCaptchaEntity>> callback){
        CheckCaptchaRequest request = new CheckCaptchaRequest();
        request.setCaptchaType("blockPuzzle");
        Point point = new Point();
        point.setY(5.0);
        point.setX(sliderXMoved);
        String pointStr = new Gson().toJson(point);
        request.setPointJson(AESUtil.encode(pointStr, key));
        request.setToken(token);
        ApiManager.getInstance().checkCaptcha(request, callback);
    }

    public void captchaCertExchange(String token, double sliderXMoved, String key, ResultCallBack<Result<CaptchaCertExchangeEntity>> callback){
        CaptchaCertExchangeRequest request = new CaptchaCertExchangeRequest();
        Point point = new Point();
        point.setY(5.0);
        point.setX(sliderXMoved);
        String pointStr = new Gson().toJson(point);
        String result= token+"---"+pointStr;
        request.setCaptcha_verification(AESUtil.encode(result,key));
//        request.setCaptcha_verification(token);
        ApiManager.getInstance().captchacertExchange(request, callback);
    }

    /**
     * 获取短信验证码
     * @param phone
     * @param token
     * @param callBack
     */
    public void loginPhoneCode(String phone, String token, ResultCallBack<Result<LoginEntity>> callBack){
        LoginRequest request = new LoginRequest();
        request.setAction("request_phone_code");
        request.setCaptcha_certificate(token);
        request.setPhone_number(phone);
        ApiManager.getInstance().loginPhoneCode(request, callBack);
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     */
    public void loginPhoneCodeCheck(String phone, String captcha_verification, String phone_code, ResultCallBack<Result<LoginEntity>> callback){
        LoginRequest request = new LoginRequest();
        request.setAction("check_phone_code");
        request.setCaptcha_certificate(captcha_verification);
        request.setPhone_number(phone);
        request.setPhone_code(phone_code);
        ApiManager.getInstance().loginPhoneCodeCheck(request, callback);
   }

    public void getQrCode(ResultCallBack<Result<QrLoginEntity>> callback){
        QrLoginRequest request = new QrLoginRequest();
        request.setVin("LSV0000000002");
        ApiManager.getInstance().qrLoginCode(request, callback);
    }

    public void userPwdModify(String password, ResultCallBack<Result> callback){
        LoginRequest request = new LoginRequest();
        request.setPassword(Sha256.getSHA256(password));
        ApiManager.getInstance().userPwdModify(request, callback);
    }

    public void loginStatus(ResultCallBack<Result<LoginStatusEntity>> callback){
        ApiManager.getInstance().loginStatus(callback);
    }

    public void login(String username, String password, String captcha_certificate, ResultCallBack<Result<LoginEntity>> callback) {
        LoginRequest request = new LoginRequest();
        request.setPhone_number(username);
        request.setPassword(Sha256.getSHA256(password));
        request.setCaptcha_certificate(captcha_certificate);
        ApiManager.getInstance().loginPhonePwd(request, callback);
    }

    public void queryUserInfo(ResultCallBack<Result<UserInfoEntity>> callback){
        ApiManager.getInstance().queryUserInfo(callback);
    }

    public void updateUserInfo(UserInfoRequest request, ResultCallBack<Result> callback){
        ApiManager.getInstance().updateUserInfo(request, callback);
    }

    public void logout(String refresh_token, ResultCallBack<Result> callback){
        LogoutRequest request = new LogoutRequest();
        request.setRefresh_token(refresh_token);
        ApiManager.getInstance().logout(request, callback);
    }
}